package com.collect.senegal.zhiScene.zhiWords;

import android.annotation.SuppressLint;

import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemDragListener;
import com.collect.senegal.R;
import com.collect.senegal.zhiAdater.zhiWords.ZHIWordsAdjuster;
import com.collect.senegal.zhiAdater.zhiWords.ZHIWordsAdjusterClickListener;
import com.collect.senegal.zhiBase.BaseActivity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRWordsEntity;
import com.collect.senegal.zhiBean.zhiSend.ZHISWordsSortEntity;
import com.collect.senegal.zhiBean.zhiSend.ZHISWordsUpdateEntity;
import com.collect.senegal.zhiBean.zhiSend.ZHISendEntity;
import com.collect.senegal.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.senegal.zhiScene.zhiWidget.zhiDialog.ZHIDialogItemClickListener;
import com.collect.senegal.zhiScene.zhiWidget.zhiDialog.ZHIDialogWordsInput;
import com.collect.senegal.zhiUtils.ToastUtil;

import java.util.ArrayList;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_WORDS)
public class ZHIWordsActivity extends BaseActivity<ZHIWordsPresenter> implements ZHIWordsContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.rc_list)
    RecyclerView rc_list;

    @BindView(R.id.iv_filter)
    ImageView iv_filter;

    @BindView(R.id.iv_products)
    ImageView iv_products;

    @BindView(R.id.btn_add)
    TextView btn_add;

    private final ZHIWordsAdjuster mZHIWordsAdjuster = new ZHIWordsAdjuster();
    private final ItemDragAndSwipeCallback mItemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mZHIWordsAdjuster);
    private final ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(mItemDragAndSwipeCallback);

    private ArrayList<ZHIRWordsEntity> mWordsEntityArrayList;
    private boolean mDidDraged = false;

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_words;
    }

    @Override
    protected void initView() {
        srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

        initEvent();
        initData(true);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onGetWordList(ArrayList<ZHIRWordsEntity> zhirWordsEntityArrayList) {
        try {
            srl_swipe_layout.setRefreshing(false);

            mWordsEntityArrayList = zhirWordsEntityArrayList;
            mZHIWordsAdjuster.setNewData(mWordsEntityArrayList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpdateWords() {
        try {
            srl_swipe_layout.setRefreshing(false);
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpdateWordsSortList() {
        try {
            srl_swipe_layout.setRefreshing(false);
            mDidDraged = false;
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("InflateParams")
    public void initEvent() {
        try {
            srl_swipe_layout.setOnRefreshListener(() -> initData(false));

            mZHIWordsAdjuster.setEmptyView(getRefreshEmptyView());
            mZHIWordsAdjuster.setZHIWordsAdjusterClickListener(new ZHIWordsAdjusterClickListener() {
                @Override
                public void onEditClick(ZHIRWordsEntity item) {
                    ZHIDialogWordsInput.showDialog(ZHIWordsActivity.this, item.getContent(), getString(R.string.pls_enter), true, new ZHIDialogItemClickListener() {
                        @Override
                        public void onConfirmClick(String content) {
                            try {
                                if (mPresenter != null) {
                                    ZHISWordsUpdateEntity zhisWordsUpdateEntity = new ZHISWordsUpdateEntity();
                                    zhisWordsUpdateEntity.setId(item.getId());
                                    zhisWordsUpdateEntity.setContent(content);
                                    zhisWordsUpdateEntity.setType(ZHIWordsReduce.TYPE_UPDATE);
                                    mPresenter.updateWords(ZHIWordsActivity.this, zhisWordsUpdateEntity);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onCancelClick() {
                            try {
                                if (mPresenter != null) {
                                    ZHISWordsUpdateEntity zhisWordsUpdateEntity = new ZHISWordsUpdateEntity();
                                    zhisWordsUpdateEntity.setId(item.getId());
                                    zhisWordsUpdateEntity.setType(ZHIWordsReduce.TYPE_DELETE);
                                    mPresenter.updateWords(ZHIWordsActivity.this, zhisWordsUpdateEntity);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }

                @Override
                public void onCopyClick() {

                }
            });

            rc_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_list.setAdapter(mZHIWordsAdjuster);
            mItemTouchHelper.attachToRecyclerView(rc_list);

            mZHIWordsAdjuster.enableDragItem(mItemTouchHelper, R.id.iv_sort_up, true);
            mZHIWordsAdjuster.enableDragItem(mItemTouchHelper, R.id.tv_content_des, true);
            mZHIWordsAdjuster.setOnItemDragListener(new OnItemDragListener() {
                @Override
                public void onItemDragStart(RecyclerView.ViewHolder viewHolder, int i) {
                    try {
                        srl_swipe_layout.setEnabled(false);
                        srl_swipe_layout.setRefreshing(false);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onItemDragMoving(RecyclerView.ViewHolder viewHolder, int i, RecyclerView.ViewHolder viewHolder1, int i1) {
                    try {
                        srl_swipe_layout.setEnabled(false);
                        srl_swipe_layout.setRefreshing(false);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onItemDragEnd(RecyclerView.ViewHolder viewHolder, int i) {
                    try {
                        srl_swipe_layout.setEnabled(true);
                        mDidDraged = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

            btn_add.setOnClickListener(v -> ZHIDialogWordsInput.showDialog(ZHIWordsActivity.this, null, getString(R.string.pls_enter), false, new ZHIDialogItemClickListener() {
                @Override
                public void onConfirmClick(String content) {
                    try {
                        if (mPresenter != null) {
                            ZHISWordsUpdateEntity zhisWordsUpdateEntity = new ZHISWordsUpdateEntity();
                            zhisWordsUpdateEntity.setContent(content);
                            zhisWordsUpdateEntity.setType(ZHIWordsReduce.TYPE_SAVE);
                            mPresenter.updateWords(ZHIWordsActivity.this, zhisWordsUpdateEntity);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onCancelClick() {

                }
            }));

            iv_filter.setOnClickListener(v -> finish());

            iv_products.setOnClickListener(v -> {
                try {
                    if (!mDidDraged) {
                        ToastUtil.s("The list sorting has not changed.");
                        return;
                    }
                    if (mPresenter != null && mWordsEntityArrayList != null && mWordsEntityArrayList.size() > 0) {
                        ArrayList<ZHISWordsSortEntity> wordsSortEntityArrayList = new ArrayList<>();

                        for (int a = 0; a < mWordsEntityArrayList.size(); a++) {
                            ZHIRWordsEntity zhirWordsEntity = mWordsEntityArrayList.get(a);

                            ZHISWordsSortEntity zhisWordsSortEntity = new ZHISWordsSortEntity();
                            zhisWordsSortEntity.setSortId(String.valueOf(a + 1));
                            zhisWordsSortEntity.setId(zhirWordsEntity.getId());
                            wordsSortEntityArrayList.add(zhisWordsSortEntity);
                        }

                        ZHISWordsUpdateEntity zhisWordsUpdateEntity = new ZHISWordsUpdateEntity();
                        zhisWordsUpdateEntity.setSortList(wordsSortEntityArrayList);
                        mPresenter.updateWordsSortList(ZHIWordsActivity.this, zhisWordsUpdateEntity);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void initData(boolean showDialog) {
        try {
            if (mPresenter != null) {
                mPresenter.getWordList(getActivity(), new ZHISendEntity(), showDialog);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> {
            initData(true);
        });
        return emptyView;
    }
}
